package com.derbysoft.dswitch.adapter.ihg.dswitch2.service.impl;

import com.derbysoft.dswitch.adapter.ihg.common.log.LogBody;
import com.derbysoft.dswitch.adapter.ihg.common.log.PerformanceLogger;
import com.derbysoft.dswitch.adapter.ihg.common.utils.EnvironmentUtils;
import com.derbysoft.dswitch.adapter.ihg.dswitch2.HotelProviderService;
import com.derbysoft.dswitch.adapter.ihg.dswitch3.translate.Translator;
import com.derbysoft.dswitch.adapter.ihg.dto.OTAHotelAvailRQ;
import com.derbysoft.dswitch.adapter.ihg.dto.OTAHotelAvailRS;
import com.derbysoft.dswitch.adapter.ihg.webservice.handler.WebServiceHandler;
import com.derbysoft.dswitch.adapter.ihg.webservice.impl.AccountProvider;
import com.derbysoft.dswitch.adapter.ihg.webservice.impl.RequestPair;
import com.derbysoft.dswitch.adapter.ihg.webservice.impl.ResponsePair;
import com.derbysoft.remote.dswitch.core.GenericRQ;
import com.derbysoft.remote.dswitch.core.GenericRS;
import com.derbysoft.remote.dswitch.core.ResponseHeader;
import com.derbysoft.remote.dswitch.hotel.dto.HotelMultiAvailRQ;
import com.derbysoft.remote.dswitch.hotel.dto.HotelMultiAvailRS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("ds2hotelMultiAvailabilityWebService")
public class HotelMultiAvailWebServiceImpl extends BaseHotelWebService
        implements HotelProviderService<GenericRQ<HotelMultiAvailRQ>, GenericRS<HotelMultiAvailRS>> {

    @Autowired
    @Qualifier("availWebServiceHandler")
    private WebServiceHandler<OTAHotelAvailRQ, OTAHotelAvailRS> hotelAvailWebService;

    @Autowired
    private Translator<GenericRQ<HotelMultiAvailRQ>, List<RequestPair<OTAHotelAvailRQ>>, Void> ds2HotelMultiAvailRQTranslatorV2;

    @Autowired
    private Translator<List<ResponsePair<OTAHotelAvailRQ, OTAHotelAvailRS>>,
            GenericRS<HotelMultiAvailRS>, GenericRQ<HotelMultiAvailRQ>> ds2HotelMultiAvailRSTranslatorV2;

    @Override
    public GenericRS<HotelMultiAvailRS> invoke(GenericRQ<HotelMultiAvailRQ> request) {
        try {
            List<RequestPair<OTAHotelAvailRQ>> otaHotelAvailRQs = ds2HotelMultiAvailRQTranslatorV2.translate(request, null);

            String derbyChannelCode = request.getHeader().getSource().getUniqueID().getId();

            List<ResponsePair<OTAHotelAvailRQ, OTAHotelAvailRS>> hotelAvailRSes = hotelAvailWebService.batchInvoke(otaHotelAvailRQs,
                    AccountProvider.of(EnvironmentUtils.getGlobalIhgAvailabilityUrl(), loadAuthorization(derbyChannelCode)));

            return ds2HotelMultiAvailRSTranslatorV2.translate(filter(hotelAvailRSes), request);
        } catch (Exception ex) {
            GenericRS<HotelMultiAvailRS> failedResponse = failedResponse(ex, request);
            PerformanceLogger.append(LogBody.ERROR_CODE, failedResponse.getErrors().getErrorList().get(0).getCode());
            return failedResponse;
        }
    }

    private GenericRS<HotelMultiAvailRS> failedResponse(Exception ex, GenericRQ<HotelMultiAvailRQ> request) {
        GenericRS<HotelMultiAvailRS> response = new GenericRS<HotelMultiAvailRS>();
        ResponseHeader header = new ResponseHeader();
        header.setTaskId(request.getHeader().getTaskId());
        response.setHeader(header);
        response.setErrors(createErrorDTO(ex));
        return response;
    }

    private List<ResponsePair<OTAHotelAvailRQ, OTAHotelAvailRS>> filter(List<ResponsePair<OTAHotelAvailRQ, OTAHotelAvailRS>> hotelAvailRSes) {
        ArrayList<ResponsePair<OTAHotelAvailRQ, OTAHotelAvailRS>> responsePairs = new ArrayList<ResponsePair<OTAHotelAvailRQ, OTAHotelAvailRS>>();
        if (hotelAvailRSes == null) {
            return responsePairs;
        }
        for (ResponsePair<OTAHotelAvailRQ, OTAHotelAvailRS> hotelAvailRS : hotelAvailRSes) {
            if (hotelAvailRS.getResponse().getSuccess() != null) {
                responsePairs.add(hotelAvailRS);
            }
        }
        return responsePairs;
    }
}
